<?php
/****
	j_list.j_state 状态：
	0 - 可以采集
	1 - 未采集到章节列表，失败
	2 - 列表已采集，开始采集章节内容中，未结束
	3 - 已采集完成 [连载中]
	4 - 已采集完成 [已完结]
	5 - 已采集完成 [未知]
*/
class PickModel extends Db{
	
	var $return = array();
	
	public function saveImage($content)
	{
		global $app;
		if(!empty($content)){
			preg_match_all('/<img.+?src="([^"]*)"/is', $content, $out);
			if(!empty($out[1])){
				$path = "static/images/site/";
				if(!file_exists($path)) mkdir($path);
				foreach($out[1] as $v){
					$v = trim($v);
					$split = explode('.', $v);
					$ext   = end($split);
					$md5File = md5($v).'.'.$ext;
					if(file_exists($path.$md5File)){
						$content = str_replace($v, '/'.$path.$md5File, $content);
					}else{
						$im = @file_get_contents($v);
						if($im){
							file_put_contents($path.$md5File, $im);
							$content = str_replace($v, '/'.$path.$md5File, $content);
						}
					}
				}
			}
		}
		return $content;
	}
	
	//采集列表
	public function picklist()
	{
		set_time_limit(180);
		global $app;	
		$return['state'] = 0;
		$return['msg']   = '';
		
		$listlink = 'http://www.zzzcn.com/{class}/{page}/';	
		
		//获取页面源代码		
		$listlink = str_replace('{page}', $app->post['page'], $listlink);
		$listlink = str_replace('{class}', $app->post['classid'], $listlink);	
		$http = new Http();
		$http->fetch($listlink);		
		
		//获取主体代码
		$html = $http->results;
		$body = cut('<div id="content">', '</table>', $html);
		
		//获取tr	
		preg_match_all('/<tr>(.+?)<\/tr>/is', $body, $trs);		
		
		//获取信息
		$list = array();
		foreach($trs[1] as $tr){
			$info = array();
			preg_match_all('/<td[^>]*>(.+?)<\/td>/i', $tr, $tds);
			$td = $tds[1];			
			$info['j_type'] = $td[0];
			$info['j_author'] = $td[3];
			preg_match('/<a href="(.+?)"[^>]*>(.+?)<\/a>/i', $td[1], $out);
			$info['j_link']  = 'http://www.zzzcn.com'.$out[1];
			$info['j_title'] = $out[2];
			
			$list[] = $info;
		}
		
		//入库
		$return['link'] = $listlink;
		$return['data'] = array();
		$i = 0;
		if(count($list) > 0){
			foreach($list as $v){
				$old = $this->find("j_list", "j_link='".$v['j_link']."'");
				if(empty($old)){
					unset($data);
					$data['j_link']  = $v['j_link'];
					$data['j_type']  = $v['j_type'];
					$data['j_title'] = $v['j_title'];
					$data['j_author'] = $v['j_author'];
					$data['j_lastdate'] = time();
					$this->add("j_list", $data);
					$return['data'][] = 'new.'.$this->insert_id() . ' ' . $v['j_title']; $i++;
				}
				else{
					$return['data'][] = 'old.'.$old['id'] . ' ' . $v['j_title'];
				}
			}
			if($i>0) 
				$return['state'] = 1; //有新记录入库
			else 
				$return['state'] = 2; //没有发现新记录		
		}
		else{
			$return['state'] = 3; //栏目结束
		}		
		
		return $return;
	}
	
	//采集内容
	function pickinfo()
	{
		set_time_limit(180);
		global $app;	
		$return['state'] = 0;
		$return['msg']   = '空';	
		
		$info = $this->find("j_list", "j_state=0");
		if(empty($info)){
			$return['state'] = 3;
		}
		else{
			//获取页面源代码		
			$listlink = $info['j_link'];
			
			$http = new Http();			
			$http->referer = $listlink;					
			$http->fetch($listlink);		
			
			//获取主体代码
			$html = $http->results;
			$body = cut('<td align="center" style="border-bottom:1px solid #a4cded; line-height:24px;">', '</td>', $html);
			$temp = explode('|', $body);
			$data['j_class'] = trim(strip_tags($temp[0]));
			$data['j_step']  = trim(strip_tags($temp[1]));
			$data['j_size']  = trim(strip_tags($temp[2]));			
			$data['j_info']  = cut('<font class="poptext">内容简介：</font>', '</td>', $html);	
			$data['j_mulu']  = cut('<li><a class="btnlink" href="', '"', $html);			
			
			if(!empty($data['j_class']) && !empty($data['j_step']) && !empty($data['j_mulu'])){
				//更新				
				$data['j_state'] = $return['state'] = 1;
				$data['j_mulu']  = 'http://www.zzzcn.com'.$data['j_mulu']; 
				$data['j_item']  = $this->pickitem( $data['j_mulu'] ); 
				
				if(!empty($data['j_item'])) 
					$data['j_state'] = 2;
				
				$this->save("j_list", "id='$info[id]'", $data);					
			}
			else{
				$data['j_state'] = 1;
				$this->save("j_list", "id='$info[id]'", $data);
				
				$return['state'] = 2;
				//echo $listlink;
				//echo $html; exit;
			}			
			$return['data']  = $data;		
		}
		
		return $return;
	}
	
	//采集章节列表
	protected function pickitem($url){
		
		//获取页面源代码	
		$http = new Http();
		$http->fetch($url);
		$html = $http->results;
		$body = cut('<table align="center" cellpadding="3" cellspacing="0" class="main acss" >', '</table>', $html);
		$lastTR = strrpos($body, "<tr>");
		if($lastTR){
			$body = substr($body, 0, $lastTR);
		}
		$lastTR = strrpos($body, "<tr>");
		if($lastTR){
			$body = substr($body, 0, $lastTR);
		}
		
		return $body;
	}
	
	//采集章节内容
	function pickiteminfo(){
		set_time_limit(180);
		global $app;	
		$return['state'] = 0;
		$return['msg']   = '';
		
		$link = '';
		$newid = 0;
		
		$info = $this->find("j_list", "j_state=2");
		if(empty($info)){
			$return['state'] = 3;
		}
		else{			
			$pick = explode(',', $info['j_pickstep']);
			$item = $info['j_item'];
			preg_match_all('/<a.+?href="(.+?)"[^>]*>/i', $item, $out);
			foreach($out[1] as $v){
				$blin = false; //默认未采集
				foreach($pick as $vv){
					$arr = explode(':', $vv);
					if( $v==$arr[1] ){
						$blin = true; break; //如果存在，标记已采集
					}
				}
				if( !$blin ){
					$link = $v; break;
				}							
			}
			
			//检查info是否有记录
			$old = $this->find("j_info", "j_listurl='$link'");		
			if(!empty($link) && !empty($old) && !empty($old['j_title'])){ //略过采集内容，直接更新进度
				$pick[] = $old['id'].':'.$link;
				unset($data);
				$data['j_pickstep'] = implode(',', $pick);
				$this->save("j_list", "id='$info[id]'", $data);
				
				$return['state'] = 1;
				$extra = count($pick).'/'.count($out[1]).' [已有info记录]'.$old['id'];			
			}
			elseif(!empty($link)){
				//获取页面源代码		
				$http = new Http();
				$http->fetch("http://www.zzzcn.com".$link);
				$html  = $http->results;
				
				$title = cut("<h2>", "</h2>", $html);
				$content  = cut('<td colspan="2" id="content3zcn">', "</td>", $html);
				if(!empty($content)){
					$content = preg_replace('/<font[^>]*>.+?<\/font>/i', '{$替换内容}', $content);
					$content = strip_tags($content, '<br> <p> <img>');
					$content = str_replace("'", "&#039;", $content);
					$content = str_replace('http://www.zzzcn.com', '<!--在线小说阅读-->', $content);
					$content = str_replace('{$替换内容}', '<!--小说阅读-->', $content);
				}
				$errorTitle = cut("<title>", "</title>", $html);			
				
				if(!empty($content) || strstr($errorTitle, "出现错误")){
					$data = array();
					$data['j_listid']  = $info['id'];
					$data['j_listurl'] = $link;
					$data['j_title']   = empty($title) ? '错误章节' : $title;
					$data['j_info']    = $content;			
					
					$return['data']  = $data;
					//采集章节内容入库
					if( !empty($old) ){
						$this->save("j_info", "id='$old[id]'", $data);
						$newid = $old['id'];
					}
					else{
						$this->add("j_info", $data);
						$newid = $this->insert_id();
					}
					
					//更新采集进度
					$pick[] = $newid.':'.$link;
					unset($data);
					$data['j_lastdate'] = time();
					$data['j_pickstep'] = implode(',', $pick);
					$this->save("j_list", "id='$info[id]'", $data);
					
					$return['state'] = 1;
					$extra = count($pick).'/'.count($out[1]);					
				}				
				else{
					echo 'title: '.$title;
					echo '<br>content: '.$content;
					echo '<br>html: '.$html;
					echo "<br>http://www.zzzcn.com".$link; exit;
					$return['state'] = 2;
					$return['data']['msg'] = $link.' 采集为空';
				}
			}
			else{
				$data = array();
				$data['j_lastdate'] = time();
				if( trim($info['j_step']) == '连载中' )
					$data['j_state'] = 3;
				elseif( trim($info['j_step']) == '已完成' )
					$data['j_state'] = 4;
				else //暂时未知
					$data['j_state'] = 5;
					
				$this->save("j_list", "id='$info[id]'", $data);
				$return['state'] = 1;
				$return['data']['msg'] = '采集状态：'.$data['j_state'];
			}
		}
		$return['data']['j_info_ID'] = $newid;
		$return['data']['j_step'] = $extra; 
		$return['data']['j_info'] = '('.strlen($return['data']['j_info']).') '.msubstr($return['data']['j_info'], 0, 50);
		$return['data']['j_link'] = $info['j_link'];
		
		return $return;
	}
}
?>